<?php
   class front_res{
    private $template;
    private $util;
    private $db;
    //variable
    private $act;
    private $id;
    private $lang;
    public function __construct(){
        require_once 'code/util.class.php';
        require_once("code/pagination.class.php");      
        $this->util = new util();
        $this->db = new db();
    }
    
    public function autorun(){
        $act =  isset($_GET['act'])?$_GET['act']:'index'; 
        $this->id = isset($_GET['id'])?$_GET['id']:0;  
        $this->lang = isset($_GET['l'])?$_GET['l']:'en';        
        switch($act){
            case 'index':
                $this->get_index();
                break;
            case 'bookonline':
                $this->get_book_form();
                break;
            case 'dobook':
                $this->do_book();
                break;
            case 'bookevent':
                $this->get_book_event_form();
                break;
            case 'dobookevent':
                $this->do_book_event();
                break;
            case 'event':
                $this->get_events();
                break;
            default:
                $this->get_index(); 
                break;   
            
        }
          
    }
     //Get book form
    private function get_events(){ 
        $this->db->openconnection();
        $sql = "SELECT * FROM event WHERE site='2' and ishow='1'";     
        $data = $this->db->run_query($sql);
        $this->db->closeconnection();
 
        $this->db->openconnection();
        $sql = "SELECT * FROM images WHERE site='2' and ishow='1'";     
        $images = $this->db->run_query($sql);
        $this->db->closeconnection();
        
        $pagination = new pagination;
        $postPages = $pagination->generate($data,2);
        
        $this->util->send_var('data',$postPages);
        $this->util->send_var('images',$images);
        $this->util->send_var('module','list_event'); 
        $this->util->send_var('pagination',$pagination->links()); 
        return ($this->util->generate_page('../templates/frontend/' . $this->lang . '/res/index.tpl')); 
    } 
     //Get book form
    private function get_book_event_form(){ 
        $this->db->openconnection();
        $sql = "SELECT * FROM images WHERE site='2' and ishow='1'";     
        $images = $this->db->run_query($sql);
        $this->db->closeconnection();
     
        $this->util->send_var('images',$images);
        $this->util->send_var('module','book_event'); 
        return ($this->util->generate_page('../templates/frontend/' . $this->lang . '/res/index.tpl')); 
    } 
     //Get book form
    private function get_book_form(){ 
        $this->db->openconnection();
        $sql = "SELECT * FROM images WHERE site='2' and ishow='1'";     
        $images = $this->db->run_query($sql);
        $this->db->closeconnection();
     
        $this->util->send_var('images',$images);
        $this->util->send_var('module','book_online'); 
        return ($this->util->generate_page('../templates/frontend/' . $this->lang . '/res/index.tpl')); 
    } 
      //Get book form
    private function do_book(){ 
         //take input
        $fullname = isset($_POST['fullname'])?$_POST['fullname']:'';
        $email = isset($_POST['email'])?$_POST['email']:'';
        $address = isset($_POST['address'])?$_POST['address']:'';
        $mobile = isset($_POST['mobile'])?$_POST['mobile']:'';
        $tel = isset($_POST['tel'])?$_POST['tel']:'';
        $date = isset($_POST['date'])?$_POST['date']:'';
        $number = isset($_POST['number'])?$_POST['number']:'1';
        
        if($fullname=='' || $email=='' || $date=='' || $number==''){
            exit();
        }
       
        //Insert database
        $str = "INSERT INTO `order`(fullname,email,address,mobile,tel,orderdate,number,site,ishow) VALUES ('$fullname','$email', '$address', '$mobile', '$tel', '$date', '$number','3',1)";        
    
        $this->db->openconnection();
       
        $id = $this->db->run_insert_query($str);
       
        $this->db->closeconnection();
        //Oke
        if($id==0){
            exit();
        }  
        //$sql = "SELECT * FROM images WHERE site='3' and ishow='1'";     
       
        //$this->db->openconnection(); 
        //$images = $this->db->run_query($sql);
        //$this->db->closeconnection();
        
        
        $this->util->send_var('date',$date); 
        $this->util->send_var('number',$number);
        //$this->util->send_var('images',$images);                       
 
        return ($this->util->generate_page('../templates/frontend/' . $this->lang . '/spa/book_result.tpl')); 
    } 
 
    //Get index
    private function get_index(){ 
        $sql = "SELECT * FROM about WHERE site='2' and ishow='1'";
        $this->db->openconnection();
        $about = $this->db->run_query($sql);
         
        $sql = "SELECT * FROM images WHERE site='2' and ishow='1'";     
       
        $images = $this->db->run_query($sql);
        $this->db->closeconnection();
        $this->util->send_var('module','about'); 
        $this->util->send_var('about',$about[0]); 
        $this->util->send_var('images',$images);
 
        return ($this->util->generate_page('../templates/frontend/' . $this->lang . '/res/index.tpl')); 
    } 
} 
?>
